// 收货地址相关接口
import request from '../utils/request.ts'
import type { ApiResponse } from './kill'

// 地址数据类型
export interface AddressPayload {
  contact: string
  mobile: string
  province: string
  city: string
  district: string
  detail: string
  isDefault: string //0为非默认，1为默认
}

// 获取地址列表
export const getAddressListService = (): Promise<ApiResponse<any[]>> => {
  // 通过类型断言，将 Axios 返回值视为统一的 ApiResponse 结构
  return request.get('/addresses') as any
}

// 新增地址
export const createAddressService = (data: AddressPayload) => {
  console.log('新增地址参数:', data)

  // 直接使用JSON格式发送，无需转换为URLSearchParams
  const headers = {
    'Content-Type': 'application/json'
  }

  // 发送JSON格式的数据
  return request.post('/addresses', data, { headers })
}

// 更新地址
export const updateAddressService = (id: string, data: Partial<AddressPayload>) => {
  return request.put(`/addresses/${encodeURIComponent(id)}`, data)
}

// 删除地址
export const deleteAddressService = (id: string) => {
  return request.delete(`/addresses/${encodeURIComponent(id)}`)
}

// 设为默认地址
export const setDefaultAddressService = (id: string) => {
  const data = {
    isDefault: '1'
  }
  return request.put(`/addresses/${encodeURIComponent(id)}/default`, data)
  //return request.patch(`/addresses/{id}/default/${encodeURIComponent(id)}`)
}


